<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var res = {
            "code": 0,
            "success": true,
            "message": "",
            "content": [
                {
                    "id": 5,
                    "pid": 0,
                    "menuType": 1,
                    "title": "系统管理",
                    "icon": "el-icon-setting",
                    "menuUrl": "/system",
                    "permissionUrl": "menu:list",
                    "menuSort": 1,
                    "componentUrl": "Layout",
                    "hasShow": true,
                    "children": [
                        {
                            "id": 7,
                            "pid": 5,
                            "menuType": 2,
                            "title": "角色管理",
                            "icon": "el-icon-user",
                            "menuUrl": "roles",
                            "permission": "roles:list",
                            "menuSort": 2,
                            "componentUrl": "system/roles",
                            "hasShow": true,
                            "children": []
                        },
                        {
                            "id": 8,
                            "pid": 5,
                            "menuType": 2,
                            "title": "菜单管理",
                            "icon": "el-icon-menu",
                            "menuUrl": "meus",
                            "permission": null,
                            "menuSort": 1,
                            "componentUrl": "system/menus",
                            "hasShow": true,
                            "children": []
                        }
                    ]
                }
            ]
        }

        var { content: data } = res;

        var hasChild = function(node) {
            return node.children && node.children.length > 0 ? true : false;
        }

        //树节点排序
        var treeSort = function(nodes, field = 'sort') {
            nodes.length == 1 && hasChild(nodes[0]) && treeSort(nodes[0].children,field);
            return nodes.sort((a, b) => {
                if(hasChild(a)){
                    treeSort(a.children,field);
                }
                if(hasChild(b)){
                    treeSort(b.children,field);
                }
                return a[field] - b[field];
            });
        };

        console.log(treeSort(data,'menuSort'));
    </script>
</body>
</html>